/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.azon.face.detection;

import java.awt.Color;

/**
 *
 * @author Mahdan Ahmad F A
 */
public class ImageDetection {
    public ImageDetection() {

    }
    
    public int[] doDetect(int[][] matrix) {
        
        int[] Left = detectLeft(0, matrix);
        int[] Top  = detectTop(0, 45, matrix);
        
        while (Math.abs(Left[0] - Top[0]) >= 45) {
            Left = detectLeft(Left[1] + 1, matrix);
        }// */
        
        int[] Right     = detectRight(Left[1] + 50, matrix);
        int[] Bottom    = detectBottom(Top[0] + 60, matrix);
        
        
        System.out.println(Top[0] + " " + Top[1]);
        System.out.println(Left[0] + " " + Left[1]);
        System.out.println(Right[0] + " " + Right[1]);
        System.out.println(Bottom[0] + " " + Bottom[1]); // */
        
        int[] arrayY = {Top[0],Left[0],Right[0],Bottom[0]};
        int[] arrayX = {Top[1],Left[1],Right[1],Bottom[1]};
        
        int[] border = {getMin(arrayY), getMax(arrayY), getMin(arrayX), getMax(arrayX)};
        
        return border;
    }
    
    public int[][] drawBox(int[] array, int[][] matrix) {
        int[][] new_matrix = new int[matrix.length][matrix[0].length];
        
        for (int m = 0; m < matrix.length; m++) {
            System.arraycopy(matrix[m], 0, new_matrix[m], 0, matrix[0].length);
        }
        
        for (int m = array[0]; m <= array[1]; m++) {
            new_matrix[m][array[2]] = Color.white.getRGB();
            new_matrix[m][array[3]] = Color.white.getRGB();
        }
        
        for (int n = array[2]; n <= array[3]; n++) {
            new_matrix[array[0]][n] = Color.white.getRGB();
            new_matrix[array[1]][n] = Color.white.getRGB();
        }
        
        return new_matrix;
    }
    
    private int getMax(int[] array) {
        int max = array[0];
        
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        
        return max;
    }
    
    private int getMin(int[] array) {
        int min = array[0];
        
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        
        return min;
    }
    
    private int[] detectTop(int start, int end, int[][] matrix) {
        int[] Top = new int[2];
        int end_checked;
        
        if ((start + end) <= matrix.length) {
            end_checked = end;
        } else {
            end_checked = matrix.length;
        }
        
        for (int m = start; m < end_checked; m++) {
            for (int n = 0; n < matrix[0].length; n++) {
                if (matrix[m][n] == 255) {
                    Top[0] = m;
                    Top[1] = n;
                    return Top;
                }
            }
        }
        return Top;
    }
    
    private int[] detectBottom(int start, int[][] matrix) {
        int[] Bottom = new int[2];
        
        int end = start - 20;
        
        for (int m = start; m > end; m--) {
            for (int n = 0; n < matrix[0].length; n++) {
                if (matrix[m][n] == 255) {
                    Bottom[0] = m;
                    Bottom[1] = n;
                    return Bottom;
                }
            }
        }
        return Bottom;        
    }
    
    private int[] detectLeft(int start, int[][] matrix) {
        int[] Left = new int[2];
        
        for (int n = start; n < matrix[0].length; n++) {
            for (int m = 0; m < matrix.length; m++) {
                if (matrix[m][n] == 255) {
                    Left[0] = m;
                    Left[1] = n;
                    return Left;
                }
            }
        }
        return Left;        
    }
        
    private int[] detectRight(int start, int[][] matrix) {
        int[] Right = new int[2];
        
        int end = start - 20;
        
        for (int n = start; n > end; n--) {
            for (int m = 0; m < matrix.length; m++) {
                if (matrix[m][n] == 255) {
                    Right[0] = m;
                    Right[1] = n;
                    return Right;
                }
            }
        }
        return Right;        
    }
    
    public int[][] doMark(int[][] matrix) {
        int[][] new_matrix = new int[matrix.length][matrix[0].length];
        
        int prev    = 0;
        int current = 0;
        
        int start = 2;
        int gap   = 25;
        
        for (int m = 0; m < matrix.length; m++) {
            new_matrix[m][0] = matrix[m][0];
            for (int n = start; n < matrix[0].length; n++) {
                prev    = matrix[m][n - start];
                current = matrix[m][n];
                
                if (Math.abs(prev - current) > gap) {
                    //new_matrix[m][n] = matrix[m][n];
                    new_matrix[m][n] = 255;
                } else {
                    new_matrix[m][n] = matrix[m][n];
                }
            }
            
        }
        
        for (int n = 0; n < matrix[0].length; n++) {
            for (int m = start; m < matrix.length; m++) {
                prev    = matrix[m - start][n];
                current = matrix[m][n];
                
                if (Math.abs(prev - current) > gap) {
                    new_matrix[m][n] = 255;
                } else {
                    //new_matrix[m][n] = matrix[m][n];
                }
            }
            
        }        
        
        return new_matrix;
    }
}
